<template>
  <el-dialog
    :title="!dataForm.fileId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="文件名" prop="fileName">
      <el-input v-model="dataForm.fileName" placeholder="文件名"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="fileCreateTime">
      <el-input v-model="dataForm.fileCreateTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="文件内容" prop="fileContent">
      <el-input v-model="dataForm.fileContent" placeholder="文件内容"></el-input>
    </el-form-item>
    <el-form-item label="是否完成导入" prop="fileImport">
      <el-input v-model="dataForm.fileImport" placeholder="是否完成导入"></el-input>
    </el-form-item>
    <el-form-item label="文件存储路径" prop="filePath">
      <el-input v-model="dataForm.filePath" placeholder="文件存储路径"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          fileId: 0,
          fileName: '',
          fileCreateTime: '',
          fileContent: '',
          fileImport: '',
          filePath: ''
        },
        dataRule: {
          fileName: [
            { required: true, message: '文件名不能为空', trigger: 'blur' }
          ],
          fileCreateTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          fileContent: [
            { required: true, message: '文件内容不能为空', trigger: 'blur' }
          ],
          fileImport: [
            { required: true, message: '是否完成导入不能为空', trigger: 'blur' }
          ],
          filePath: [
            { required: true, message: '文件存储路径不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.fileId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.fileId) {
            this.$http({
              url: this.$http.adornUrl(`/rsfile/bak/info/${this.dataForm.fileId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.fileName = data.bak.fileName
                this.dataForm.fileCreateTime = data.bak.fileCreateTime
                this.dataForm.fileContent = data.bak.fileContent
                this.dataForm.fileImport = data.bak.fileImport
                this.dataForm.filePath = data.bak.filePath
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/rsfile/bak/${!this.dataForm.fileId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'fileId': this.dataForm.fileId || undefined,
                'fileName': this.dataForm.fileName,
                'fileCreateTime': this.dataForm.fileCreateTime,
                'fileContent': this.dataForm.fileContent,
                'fileImport': this.dataForm.fileImport,
                'filePath': this.dataForm.filePath
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
